home *** CD-ROM | disk | FTP | other *** search
- unit CrossF;
-
- interface
-
- uses
- Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
- StdCtrls, FileCtrl, ComCtrls;
-
- type
- TFormCrossRef = class(TForm)
- FileListBox1: TFileListBox;
- ButtonFiles: TButton;
- EditPath: TEdit;
- ListBoxFiles: TListBox;
- ButtonWords: TButton;
- ButtonHtml: TButton;
- lbList: TListBox;
- ProgressBar1: TProgressBar;
- lbSkip: TListBox;
- Button1: TButton;
- PageControl1: TPageControl;
- Label1: TLabel;
- EditBookDescription: TEdit;
- Label2: TLabel;
- procedure ButtonFilesClick(Sender: TObject);
- procedure ButtonWordsClick(Sender: TObject);
- procedure LbWordsDblClick(Sender: TObject);
- procedure LbWordsClick(Sender: TObject);
- procedure ButtonHtmlClick(Sender: TObject);
- procedure Button1Click(Sender: TObject);
- procedure FormCreate(Sender: TObject);
- procedure FormClose(Sender: TObject; var Action: TCloseAction);
- private
- LetterLists: array ['A'..'Z'] of TListBox;
- public
- procedure ExamineDir (Mask: string);
- end;
-
- var
- FormCrossRef: TFormCrossRef;
-
- implementation
-
- uses
- Newparse;
-
- {$R *.DFM}
-
- // support functions, borrowed from HTTP
-
- function TranslateChar(const Str: string; FromChar, ToChar: Char): string;
- var
- I: Integer;
- begin
- Result := Str;
- for I := 1 to Length(Result) do
- if Result[I] = FromChar then
- Result[I] := ToChar;
- end;
-
- function DosPathToUnixPath(const Path: string): string;
- begin
- Result := TranslateChar(Path, '\', '/');
- end;
-
- // form code
-
- procedure TFormCrossRef.ButtonFilesClick(Sender: TObject);
- begin
- ListBoxFiles.Items.Clear;
- {list all the files with the following extensions
- in the ListBoxFiles list box, looking in all the
- sub-directories of the indicated path}
- FileListbox1.Directory := EditPath.Text;
- ExamineDir ('*.pas');
- ExamineDir ('*.dpr');
- ExamineDir ('*.dpk');
- Beep;
- end;
-
- procedure TFormCrossRef.ExamineDir (Mask: string);
- var
- FileList: TStrings;
- I: Integer;
- CurrDir: string;
- begin
- FileListBox1.Mask := Mask;
- FileListBox1.FileType := [ftNormal];
- FileList := TStringList.Create;
- try
- FileList.Assign(FileListBox1.Items);
- // for each file, add its path to the list
- for I := 0 to FileList.Count - 1 do
- begin
- ListBoxFiles.Items.Add (FileListbox1.Directory +
- '\' + FileList[I]);
- end;
- // examine sub directories
- FileListBox1.Mask := '*.*';
- FileListBox1.FileType := [ftDirectory];
- FileList.Assign(FileListBox1.Items);
- CurrDir := FileListbox1.Directory;
- for I := 2 to FileList.Count - 1 do
- begin
- // for each directory, re-examine (recursive call)
- FileListbox1.Directory :=
- CurrDir + '\' + Copy (FileList[I], 2, Length (FileList [I]) - 2);
- ExamineDir (Mask);
- Application.ProcessMessages;
- end;
- FileListbox1.Directory := CurrDir;
- finally
- FileList.Free;
- end;
- end;
-
- procedure TFormCrossRef.ButtonWordsClick(Sender: TObject);
- var
- CurrFile, TokenStr: string;
- I, Item, Idx: Integer;
- FileText: TStream;
- Parse: TNewParser;
- sList: TStringList;
- // StartTime: TTime;
- LettList: TListBox;
- begin
- // StartTime := Now;
- // for each file listed
- ProgressBar1.Max := ListBoxFiles.Items.Count - 1;
- for I := 0 to ListBoxFiles.Items.Count - 1 do
- begin
- // select the current file, to show the progress
- ListBoxFiles.ItemIndex := I;
- // get the current file
- CurrFile := ListBoxFiles.Items [I];
- // open it as a text file
- FileText := TFileStream.Create (CurrFile, fmOpenRead);
- // pass the file to the custom parser
- Parse := TNewParser.Create (FileText);
- try
- while Parse.Token <> toEOF do
- begin
- case Parse.Token of
- // ignore strings, comments, symbols...
- toSymbol:
- begin
- TokenStr := Parse.TokenString;
- // more than one character
- if (Length (TokenStr) > 1) and
- // doesn't end with a number
- (TokenStr [Length (TokenStr)] > 'A') and
- // not in the skip list
- (lbSkip.Items.IndexOf (TokenStr) < 0) then
- begin
- // get the listbox for the current letter
- LettList := LetterLists[Upcase(TokenStr[1])];
- // look if the token is already in the list of found tokens
- // for the current letter
- Item := LettList.Items.IndexOf (TokenStr);
- if Item < 0 then
- begin
- // if not, create a new string list for the files
- sList := TStringList.Create;
- sList.Sorted := True;
- sList.Add (CurrFile);
- // add the new word and the string list
- LettList.Items.AddObject (TokenStr, sList);
- end
- else
- begin
- // add the new file reference
- sList := TStringList(LettList.Items.Objects[Item]);
- Idx := sList.IndexOf (CurrFile);
- if Idx < 0 then
- sList.Add (CurrFile);
- end;
- end;
- end;
- end;
- Parse.NextToken;
- Application.ProcessMessages;
- end;
- finally
- Parse.Free;
- FileText.Free;
- end;
- ProgressBar1.Position := I;
- end;
- Beep;
- // ShowMessage ('Elapsed: ' + TimeToStr (StartTime - Now));
- end;
-
- procedure TFormCrossRef.LbWordsDblClick(Sender: TObject);
- begin
- // move to the list of skip items
- with (Sender as TListBox) do
- begin
- // destroy the connected string list
- TStringList (Items.Objects [ItemIndex]).Free;
- // add the item to the skip list
- lbSkip.Items.Add (Items[ItemIndex]);
- // remove the item
- Items.Delete (ItemIndex);
- end;
- end;
-
- procedure TFormCrossRef.LbWordsClick(Sender: TObject);
- begin
- // show the list of files
- with (Sender as TListBox) do
- lbList.Items := TStringList (Items.Objects [ItemIndex]);
- end;
-
- // create the HTML files...
- procedure TFormCrossRef.ButtonHtmlClick(Sender: TObject);
- var
- Dest: TStream;
- HTML, OutFileName: string;
- I, J: Integer;
- Letter: Char;
- sList: TStringList;
- begin
- FileListBox1.Mask := '*.dpr';
- FileListBox1.FileType := [ftNormal];
- SetLength (HTML, 10000);
- // for each letter
- for Letter := 'A' to 'Z' do
- begin
- // select the tab sheet we are working on
- PageControl1.ActivePage :=
- LetterLists [Letter].Parent as TTabSheet;
- HTML := '';
- // add head
- HTML :=
- '<HTML><HEAD>' + #13#10 +
- '<TITLE>CrossReference</TITLE>' + #13#10 +
- '<META NAME="GENERATOR" CONTENT="CrossRef[Marco Cant∙]">' + #13#10 +
- '</HEAD>'#13#10 +
- '<BODY>'#13#10 +
- '<CENTER><I>' + EditBookDescription.Text
- + '</I></CENTER></H3><BR><BR>'#13#10 +
- '<H1><CENTER>Cross Reference: ' +
- Letter + '</CENTER></H1><BR><BR><HR>'#13#10;
- // for each identifier starting with the letter
- for I := 0 to LetterLists [Letter].Items.Count - 1 do
- begin
- Application.ProcessMessages;
- // add the word
- AppendStr (HTML, '<H4>' + LetterLists [Letter].
- Items[I] + '</H4>'#13#10);
- // sub-list
- AppendStr (HTML, '<UL>'#13#10);
- sList := TStringList (LetterLists [Letter].Items.Objects [I]);
- // add file names
- for J := 0 to sList.Count - 1 do
- AppendStr (HTML, '<LI><A HREF="' +
- ChangeFileExt (DosPathToUnixPath (
- Copy (sList[J], Length (EditPath.Text) + 1, 1000)),
- '_' + Copy (ExtractFileExt(sList[J]), 2, 3)) +
- '.htm ">' +
- Copy (sList[J], Length (EditPath.Text) + 1, 1000) +
- '</A>'#13#10);
- AppendStr (HTML, '</UL>'#13#10);
- end;
- // add tail
- AppendStr (HTML,
- '<BR><I><CENTER>' +
- 'File generated by CrossRef, a tool by Marco Cantù' +
- '</CENTER></I>'#13#10 +
- '</BODY> </HTML>');
- // create the output file
- OutFileName := EditPath.Text + Letter + '.htm';
- Dest := TFileStream.Create (OutFileName,
- fmCreate or fmOpenWrite or fmShareDenyNone);
- try
- Dest.WriteBuffer (Pointer(HTML)^, Length (HTML));
- finally
- Dest.Free;
- end;
- end; // for Letter
- Beep; // done
- end;
-
- procedure TFormCrossRef.Button1Click(Sender: TObject);
- begin
- with TSaveDialog.Create (nil) do
- begin
- if Execute then
- LbSkip.Items.SaveToFile (FileName);
- Free;
- end;
- end;
-
- procedure TFormCrossRef.FormCreate(Sender: TObject);
- var
- Letter: Char;
- List : TListBox;
- Sheet: TTabSheet;
- begin
- // create 26 list boxes, and connects them...
- for Letter := 'A' to 'Z' do
- begin
- Sheet := TTabSheet.Create (self);
- Sheet.PageControl := PageControl1;
- Sheet.Caption := Letter;
- List := TListBox.Create (self);
- List.Parent := Sheet;
- List.Align := alClient;
- List.Sorted := True;
- List.OnClick := LbWordsClick;
- List.OnDblClick := LbWordsDblClick;
- LetterLists [Letter] := List;
- end;
- end;
-
- procedure TFormCrossRef.FormClose(Sender: TObject;
- var Action: TCloseAction);
- begin
- Action := caFree;
- FormCrossRef := nil;
- end;
-
- end.
-